package com.min.star.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.min.star.model.Student;
import com.min.star.service.IStudentService;
import com.min.star.ui.Json;

@Controller
@RequestMapping("/student")
public class StudentController {

	@Autowired
	private IStudentService studentService;
	
	/*
	 * 跳转到 查询页面
	 */
	@RequestMapping("/query")
	public String query(Model model) {
		model.addAttribute("student", new Student());
		return "/admin/students";
	}
	@RequestMapping("/meesage")
	public String meesage(Model model) {
		model.addAttribute("student", new Student());
		return "/student/studentList";
	}
	
	@RequestMapping("/info")
	public String info(Model model,HttpServletRequest request) {
		Student student = (Student) request.getSession().getAttribute("student");
		Student info = this.studentService.selectById(student.getId());
		model.addAttribute("student", info);
		System.out.println(info.toString());
		return "/student/studentPersonal";
	}
	
	//个人信息页面编辑
	@RequestMapping("/edit/{data}")
	public String editInfo(@PathVariable("data") List<Object> data, Model model,HttpServletRequest request,HttpSession session) {
		Student student = (Student) request.getSession().getAttribute("student");
		System.out.println(student.getId());
		int ret = studentService.editInfo(data.get(0), data.get(1),student.getId());
		Student info = this.studentService.selectById(student.getId());
		model.addAttribute("student", info);
		String  message = null;
		message = ret > 0 ? "保存成功":"保存失败";
		session.setAttribute("message", message);
		return "/student/studentPersonal";
	} 
	
	@RequestMapping("/list")
	@ResponseBody //将方法的返回值序列化json格式并返回前台
	public Object list(Student student) {
		//通过Page<Student>对象设置查询条件,指定分页的页码和页的大小
	    Page<Student> page = new Page<>(student.getPageNumber(),student.getPageSize()); 
	    //通过page条件查询出来的记录存放到另一个Page对象 Page<Student> pager =
		Page<Student> pager = this.studentService.findPage(page,student);
		Map<String,Object> map = new HashMap<>();
		map.put("rows", pager.getRecords());
		map.put("total", pager.getTotal());
		return map;
	}
	/**
	 * 根据id删除记录
	 * @param publisherId
	 * @return
	 */
	@RequestMapping("/delete/{ids}")
	@ResponseBody
	public Object delete(@PathVariable("ids") String ids) {
		Json j = new Json();
		int ret = this.studentService.delete(ids);
		if(ret > 0) {
			j.setMsg("删除成功！");
			j.setCode(0);
		}else {
			j.setMsg("删除失败！");
			j.setCode(-1);
		}
		return j;
	}
	
	
	@RequestMapping("/add")
	@ResponseBody
	public Object add(@Valid Student student,BindingResult result) {
		 Json j = new Json();
		//服务端验证
			if(result.hasErrors()) {
				return validMessage(result, j);
			}
		 //save返回的是新增对象
		 Student info = this.studentService.save(student);
		  if(info.getId() > 0) { 
			j.setMsg("新增成功！"); 
			j.setObj(info);
		    j.setCode(0); 
		  }else {
		    j.setMsg("新增失败！");
		     j.setCode(-1);
		  } 
		  return j;
	}
	
	//新增服务端验证打包
	private Object validMessage(BindingResult result, Json j) {
		String msg="";
		List<ObjectError> allErrors = result.getAllErrors();
		if(allErrors!=null && allErrors.size()>0) {
			for(int i=0;i<allErrors.size();i++) {
				ObjectError error = allErrors.get(i);
				if(i == 0) {
					msg += error.getDefaultMessage() + "<br/>";
				}else {
					msg += "<i class='mdi mdi-emoticon-sad'></i>" + error.getDefaultMessage() + "<br/>";
				}
			}
		}
		j.setMsg(msg);
		j.setCode(-1);
		j.setObj(allErrors);
		return j;
	}
	
	@RequestMapping("/edit")
	@ResponseBody
	public Object edit(@Valid Student student, BindingResult result) {
		Json j = new Json();
		//服务端验证
		if(result.hasErrors()) {
			return validMessage(result, j);
		}
		//
		 //save返回的是新增对象
		 Student info = this.studentService.update(student);
		  if(info.getId() > 0) { 
			j.setMsg("编辑成功！"); 
			j.setObj(info);
		    j.setCode(0); 
		  }else {
		    j.setMsg("编辑失败！");
		     j.setCode(-1);
		  } 
		  return j;
	}
}
